package cn.tedu.v3;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 聊天室version 3
 * 功能：一个客户端与一个服务器端相互聊天
 * 1.客户端--链接服务器--第一件事情：写出
 * 2.客户端--读入--存线程中，对应服务器的读入
 * <p>
 * 192.168.166.8
 * 192.168.184.17
 */
public class Client {
    private Socket socket;
    //私有化IP
    private String ip;
    BufferedReader br=null;
    public Client() {
        try {
            socket = new Socket("192.168.166.8", 1234);
            //2.确认传入IP就是当前IP地址
            ip = socket.getInetAddress().getHostAddress();
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开连接");
        }
    }

    //1.start方法：IO流操作：写出功能，实习读入功能交给线程处理
    public void start() {
        PrintWriter pw = null;
        //1.6置空流-赋值、关闭流【br 作用域】
       // BufferedReader br =null;
        try {
            //1.2读入操作
            //InputStream is=socket.getInputStream();
            // InputStreamReader isr=new InputStreamReader(is,"UTF8");
            // BufferedReader br=new BufferedReader(isr);
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            //1.1写出操作
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
            //1.3创建线程---Lambda表达式--读入功能--存线程中
             new Thread(() -> {
                try {
                    //1.4线程中创建变量--接收服务器发来的文本
                    String message;
                    //1.5一直接收客户端发来的消息：massage只要与数据一直输出
                    while ((message = br.readLine()) != null) {
                        System.out.println( message);
                        System.out.println("客户端请输入：");
                    }
                } catch (IOException e) {
                    System.out.println("服务器端发来的信息断开了");
                }
            }).start();
               //1.5增加写操作【修改目的：读入写出通顺】
            //System.out.println("客户端用户:请输入文字:");
            //1.5.1控制台卡住--输入内容
            Scanner input = new Scanner(System.in);
            //1.5.2一直进行写出
            while (true) {
                System.out.println("客户端请输入");
                String say = input.nextLine();
                pw.println("客户端说：" + say);
                //System.out.println("写出完毕");
            }

        } catch (UnsupportedEncodingException e) {
            System.out.println("客户端与服务器端：字符集不一致");
        } catch (IOException e) {
            System.out.println("客户端断开连接！");
        } finally {
            if (br!=null)
                try {
                   br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (pw != null)
                pw.close();
            if (socket != null && !socket.isClosed()) {
                System.out.println("客户端关闭成功！");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
